
const path = require('path');
const pluginsConfig = require("./webpack.plugins.js");
const ExtractTextPlugin = require('extract-text-webpack-plugin');

// 设置入口文件
const glob = require('glob');
var entries = function () {
    var jsDir = path.resolve(__dirname, 'src/js')
    var entryFiles = glob.sync(jsDir + '/**/*.{js,jsx}')
    var map = {};
    
    for (var i = 0; i < entryFiles.length; i++) {
        var filePath = entryFiles[i];
        var filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'));
        map[filename] = filePath;
    }
    return map;
}

module.exports = {
    entry:  entries(),
    output: {
      path: path.resolve(__dirname, 'dist'), // 打包后的根目录
      filename: 'js/[name].[chunkhash].js',
    },
    externals: {
      jquery: "jQuery",
      template: "template"
    },
    // optimization: {
    //     splitChunks: {
    //         cacheGroups: {
    //             lib: {
    //                 name: "abc",
    //                 chunks: "initial",
    //                 enforce:true   // 强制
    //             }
    //         }
    //     }
    // },
    devtool: process.env.NODE_ENV == "production" ? false:'inline-source-map',
    devServer: {
        contentBase: './dist',
        inline: true,
        port: 9000,
        open: true,
    },
    plugins: pluginsConfig,
    module: {
      rules: [
        {
          test: /\.(css|scss|sass)$/,
          // 使用postcss不分离的写法
          //use: ["style-loader", "css-loader", "sass-loader","postcss-loader"]
          use: ExtractTextPlugin.extract({
            fallback: 'style-loader',
            use: ['css-loader', 'sass-loader',"postcss-loader"],
            publicPath:'../' //解决css背景图的路径问题
          })
        },
        {
          test: /\.js$/,
            loader: "babel-loader",
            exclude:[
              path.resolve(__dirname, 'node_modules'),
              path.resolve(__dirname, 'src/js/lib')
            ],
            include: path.resolve(__dirname, 'src')
        },
        {
          test:/\.(png|jpg|gif)$/,
          use: [
            {
              loader: 'url-loader',
              options:{ // 这里的options选项参数可以定义多大的图片转换为base64
                limit:50000, // 表示小于50kb的图片转为base64,大于50kb的是路径
                name: '[name].[ext]?[hash]',
                outputPath:'images/' //定义输出的图片文件夹
              }
            },
            {
              loader: 'image-webpack-loader', // 压缩图片
              options: {
                bypassOnDebug: true,
              }
            }
          ]
        },
        {
          test: /\.html$/,
          use: [ {
            loader: 'html-loader',
            options: {
              minimize: true
            }
          }],
        }
      ]
    }
};